package com.service.web;

import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.NavigationCase;
import javax.faces.bean.ManagedBean;
import org.primefaces.model.menu.DefaultMenuItem;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.DefaultSubMenu;
import org.primefaces.model.menu.MenuModel;

import com.service.module.Module;

@ManagedBean(name = "menuBean")
public class MenuBean implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private MenuModel model;

	public MenuBean() {
		try {
			ContextoBean contextoBean = ContextoUtil.getContextoBean();
			List<Module> listModules = contextoBean.getListModules();
			model = new DefaultMenuModel();

			for (Module module : listModules) {
				DefaultSubMenu submenu = new DefaultSubMenu(module.getName());
				List<Module> subModule2 = module.getSubModule();
				for (int i = 0; i < subModule2.size(); i++) {
					Module subModule = subModule2.get(i);
					DefaultMenuItem item = new DefaultMenuItem(
							subModule.getName());
					item.setAjax(true);
					item.setUpdate("@([id$=content])");
					item.setCommand("#" + "{" + subModule.getController() + "."
							+ subModule.getAction() + "}");
					item.setIcon(subModule.getIcon());
					submenu.addElement(item);
				}
				model.addElement(submenu);
			}
		} catch (Throwable e) {
			Logger.getLogger(NavigationCase.class.getName()).log(Level.SEVERE,
					null, e);
		}
	}

	public MenuModel getModel() {
		return model;
	}

	public String redirect() {
		ContextoBean contextoBean = ContextoUtil.getContextoBean();
		contextoBean.resetTimer();
		return "/restrito/principal";
	}
	
	public String manage(){
		ContextoBean contextoBean = ContextoUtil.getContextoBean();
		contextoBean.resetTimer();
		return "/admin/module/manage_module";
	}
}

